import libtcodpy as libtcod

import console

class Intro(object):
    def __init__(self, root, version):
        self.root = root

        banner = r"""
      ____                         ____                  __
     / __ \____  ____ ___  _____  / __ )____ _____  ____/ /
    / /_/ / __ \/ __ `/ / / / _ \/ __  / __ `/ __ \/ __  / 
   / _, _/ /_/ / /_/ / /_/ /  __/ /_/ / /_/ / / / / /_/ /  
  /_/ |_|\____/\__, /\__,_/\___/_____/\__,_/_/ /_/\__,_/   
              /____/                                       
  
                __  __                    __
               / / / /___  _________     / /
              / /_/ // _ \/ ___/ __ \   / / 
             / __  //  __/ /  / /_/ /  /_/  
            /_/ /_/ \___/_/   \____/  (_)   
""".split('\n')

        credits = ['Written by Daryl Fox <rts.cts@gmail.com>',
                   'Mad Licks by Steve [REDACTED] <http://www.thesilverbacks.org>',
                   "Additional songs by Shawna's Crew.  You all rock.",
                   'libtcod-1.5.0 <http://doryen.eptalys.net/libtcod>',
                   'http://sites.google.com/site/rogebandhero/',
                  ] 
        self.banner = console.Console(root.width, root.height)
        banner_len = max((len(line) for line in banner))
        x = max(0, (root.width - banner_len) / 2)
        self.banner.foreground = libtcod.red
        for ix_banner, line in enumerate(banner):
            self.banner.print_left(x, ix_banner + 1, libtcod.BKGND_SET, line)

        self.banner.foreground = libtcod.grey
        for ix_credit, credit in enumerate(credits):
            y = 2 + ix_banner + ix_credit
            self.banner.print_center(root.width / 2, y, libtcod.BKGND_SET, credit)

        self.banner.forground = libtcod.light_grey
        self.banner.print_right(root.width-1, root.height-1, libtcod.BKGND_SET, version)

        self._log = console.Console(root.width - 6, root.height * 3 / 2)
        self.messages = []
        map_colors = [libtcod.Color(32, 32, 32),
                      libtcod.Color(32, 32, 32),
                      libtcod.Color(255, 255, 255),
                     ]
        map_ix = [0, self._log.height-7, self._log.height-1]
        self.msg_colors = libtcod.color_gen_map(map_colors, map_ix)

        self.draw()

    def draw(self):
        self._log.blit(0, 0, self._log.width, self._log.height, self.root, 3, self.root.height - self._log.height - 1)
        self.banner.blit(0, 0, self.banner.width, self.banner.height, self.root, 0, 0, background_alpha=0)
        self.root.flush()

    def log(self, message):
        self.messages.append(message)

        self._log.clear()
        y = self._log.height
        for m in self.messages[::-1]:
            y -= 1
            self._log.foreground = self.msg_colors[y]
            self._log.print_center(self.root.width / 2 - 1, y, libtcod.BKGND_NONE, m)
            if y < 0: break

        self.draw()


if __name__ == '__main__':
    import time
    import random

    libtcod.sys_set_fps(30)
    libtcod.console_set_custom_font('fonts/arial10x10.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
    root = console.RootConsole(80, 40, 'Intro Test')

    intro = Intro(root)
    intro.draw()

    for i in range(30):
        pad = "#" * random.randrange(8)
        intro.log("%s Message %d %s" % (pad, i, pad))
        intro.draw()
        time.sleep(0.25)

    print "Press and key."
    root.wait_for_keypress(True)

        






        

